package com.dhnsoft.education.service.edu.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.edu.EduChapter;
import com.dhnsoft.education.entity.edu.EduVideo;
import com.dhnsoft.education.entity.edu.chapter.ChapterVo;
import com.dhnsoft.education.entity.edu.chapter.VideoVo;
import com.dhnsoft.education.handle.EduException;
import com.dhnsoft.education.mapper.edu.EduChapterMapper;
import com.dhnsoft.education.service.edu.EduChapterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-19
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private com.dhnsoft.education.service.edu.EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //查询章节信息
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = this.baseMapper.selectList(chapterQueryWrapper);
        //查询小节信息
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(videoQueryWrapper);
        //最终要的数据列表
        List<ChapterVo> courseInfoVos = new ArrayList<>();
        //填充章节vo数据
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            //创建章节vo对象
            ChapterVo courseInfoVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,courseInfoVo);
            courseInfoVos.add(courseInfoVo);
            //填充小节vo对象
            List<VideoVo> videoVos = new ArrayList<>();
            for (int m = 0; m < eduVideos.size(); m++) {
                EduVideo eduVideo = eduVideos.get(m);
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            courseInfoVos.get(i).setChildren(videoVos);

        }

        return courseInfoVos;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count>0){
            //能查询出来小节，不进行删除
            throw new EduException(20001,"还有小节数据，不能删除");
        }else {
            //不能查询出小节，进行删除
            int delete = baseMapper.deleteById(chapterId);
            return delete>0;
        }

    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        baseMapper.delete(chapterQueryWrapper);
    }
}
